/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.deployment.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Deployment;
import org.eclipse.uml2.uml.DeploymentTarget;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.IGraphEdgeHelper;
import com.cea.papyrus.core.helper.IModelerHelper;
import com.cea.papyrus.core.ui.dialogs.ChooseSetClassifiersDialog;

/**
 * 
 */
public class NodeInstanceSpecificationHelper extends com.cea.papyrus.diagram.classdiagram.helper.NodeInstanceSpecificationHelper{

	/**
	 * The deployment created.
	 */
	private boolean deploymentCreated;
	
	/**
	 * The element factory.
	 */
	private Object elementFactory;
	
	/**
	 * The imodeler factory.
	 */
	private IModelerHelper imodelerFactory;
	
	/**
	 * The the deployment.
	 */
	private Deployment theDeployment;

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecuteCreateElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		boolean canCreate = super.canCreateUMLElement(umlParent);
		if ( canCreate || (umlParent instanceof DeploymentTarget)) {
			return true;
		}
		return canCreate;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#addElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		boolean isAdded = super.addUMLElement(element, umlParent);
		if ( ( isAdded == false) && (umlParent instanceof DeploymentTarget) ) {	
			// Get the right helper
			imodelerFactory= PapyrusPlugin.getDefault().getFactoryRegistry().getFactory(Deployment.class);
			elementFactory = imodelerFactory.getElementFactory(Deployment.class);			
	
			// No deployment...so let's create one
			if ( ((DeploymentTarget)umlParent).getDeployments().isEmpty() ) {			
				// Create the deployment
				Element elementCreated = ((IGraphEdgeHelper)elementFactory).createUMLElement( umlParent);			
				theDeployment = ((Deployment)elementCreated);
				deploymentCreated = true;
			} else {
				// Get the last deployment
				theDeployment = (Deployment)((DeploymentTarget)umlParent).getDeployments().get(((DeploymentTarget)umlParent).getDeployments().size() - 1);				
				deploymentCreated = false;
			}
			
			// Connect the deployment
			((IGraphEdgeHelper)elementFactory).connect(umlParent, element, theDeployment);			
		
			// Add instance specification to the nearest package
			return ((DeploymentTarget) umlParent).getNearestPackage().getPackagedElements().add((PackageableElement)element);		
		}
		return isAdded;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public  Element createUMLElement(String name,  Element umlParent){
		InstanceSpecification anInstanceSpecification; 
		if (umlParent instanceof DeploymentTarget) {
			anInstanceSpecification = UMLFactory.eINSTANCE.createInstanceSpecification();
			anInstanceSpecification.setName(name);
			
			// Get the right helper
			imodelerFactory= PapyrusPlugin.getDefault().getFactoryRegistry().getFactory(Deployment.class);
			elementFactory = imodelerFactory.getElementFactory(Deployment.class);			
			
			// No deployment...so let's create one
			if ( ((DeploymentTarget)umlParent).getDeployments().isEmpty() ) {			
				// Create the deployment
				Element element = ((IGraphEdgeHelper)elementFactory).createUMLElement( umlParent);			
				theDeployment = ((Deployment)element);
				deploymentCreated = true;
			} else {
				// Get the last deployment
				theDeployment = (Deployment)((DeploymentTarget)umlParent).getDeployments().get(((DeploymentTarget)umlParent).getDeployments().size() - 1);				
				deploymentCreated = false;
			}
			
			// Connect the deployment
			((IGraphEdgeHelper)elementFactory).connect(umlParent, anInstanceSpecification, theDeployment);			
			// Add artifact to the nearest package
			((DeploymentTarget) umlParent).getNearestPackage().getPackagedElements().add(anInstanceSpecification);
		} else {
			anInstanceSpecification	= (InstanceSpecification) super.createUMLElement(name, umlParent);
		}
		
		// Set Classifier to instanceSpecification
		ChooseSetClassifiersDialog dialog = new ChooseSetClassifiersDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), anInstanceSpecification);
		int val = dialog.open();
		if (val == ChooseSetClassifiersDialog.OK) {
			anInstanceSpecification.getClassifiers().addAll(dialog.getSelectedElements()); 
		}
					
		return anInstanceSpecification;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#removeElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof DeploymentTarget){			
			// Get the right helper
			imodelerFactory= PapyrusPlugin.getDefault().getFactoryRegistry().getFactory(Deployment.class);
			elementFactory = imodelerFactory.getElementFactory(Deployment.class);	
			
			// Deconnect the deployment
			((IGraphEdgeHelper)elementFactory).deconnect(umlParent, element, theDeployment);					
			
			// Remove the deployment if created
			if (deploymentCreated){			
				((DeploymentTarget)umlParent).getClientDependencies().remove(theDeployment);
			}
			// Remove deployment target from the nearest package
			((DeploymentTarget)umlParent).getNearestPackage().getPackagedElements().remove(element);			
		}
		if (umlParent instanceof Package) {				
			return ((Package)umlParent).getPackagedElements().remove(element);
		}
		return false;
	}

	/**
	 * Gets the graph node size.
	 * 
	 * @return the graph node size
	 */
	public Dimension getGraphNodeSize() {
		return new Dimension(200,100);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#getNewUMLElementName(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public String getNewUMLElementName(Element umlParent) {
		String name = ""; //$NON-NLS-1$
	
		// i <10000: avoid infinite loops
		for(int i=0; i< 10001; i++ ) {
			name = getBaseString()+i;
			if (umlParent instanceof Package){
				if(null == ((Package)umlParent).getPackagedElement(name)) {
					return name;
				}
			}
			if (umlParent instanceof DeploymentTarget){
				if(null == ((DeploymentTarget)umlParent).getNearestPackage().getPackagedElement(name)) {
					return name;
				}
			}
		}
		return getBaseString()+"X"; //$NON-NLS-1$
	}
}
